package com.ics.atable.chat.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ics.atable.chat.model.dto.SqlReplaceDTO;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class ReplaceDeserializer extends JsonDeserializer<SqlReplaceDTO.RunRequest.Replace> {

    @Override
    public SqlReplaceDTO.RunRequest.Replace deserialize(JsonParser p, DeserializationContext ctxt) 
            throws IOException, JsonProcessingException {
        
        SqlReplaceDTO.RunRequest.Replace replace = new SqlReplaceDTO.RunRequest.Replace();
        ObjectNode node = p.getCodec().readTree(p);
        
        // 直接设置各个字段，替代原来的replaceMap
        JsonNode dataCellUuidNode = node.get("dataCellUuid");
        if (dataCellUuidNode != null && dataCellUuidNode.isTextual()) {
            replace.setDataCellUuid(dataCellUuidNode.asText());
        }
        
        JsonNode parentItemTableRelationIdNode = node.get("parentItemTableRelationId");
        if (parentItemTableRelationIdNode != null && parentItemTableRelationIdNode.isTextual()) {
            replace.setParentItemTableRelationId(parentItemTableRelationIdNode.asText());
        }
        
        JsonNode parentDataTableUuidNode = node.get("parentDataTableUuid");
        if (parentDataTableUuidNode != null && parentDataTableUuidNode.isTextual()) {
            replace.setParentDataTableUuid(parentDataTableUuidNode.asText());
        }
        
        JsonNode cellValueNode = node.get("cellValue");
        if (cellValueNode != null && cellValueNode.isTextual()) {
            replace.setCellValue(cellValueNode.asText());
        }
        
        JsonNode provideOrgIdNode = node.get("provideOrgId");
        if (provideOrgIdNode != null && provideOrgIdNode.isTextual()) {
            replace.setProvideOrgId(provideOrgIdNode.asText());
        }
        
        JsonNode provideAdminLevelNode = node.get("provideAdminLevel");
        if (provideAdminLevelNode != null && provideAdminLevelNode.isTextual()) {
            replace.setProvideAdminLevel(provideAdminLevelNode.asText());
        }
        
        JsonNode parentMouldColumnUuidNode = node.get("parentMouldColumnUuid");
        if (parentMouldColumnUuidNode != null && parentMouldColumnUuidNode.isTextual()) {
            replace.setParentMouldColumnUuid(parentMouldColumnUuidNode.asText());
        }
        
        JsonNode itemNameNode = node.get("itemName");
        if (itemNameNode != null && itemNameNode.isTextual()) {
            replace.setItemName(itemNameNode.asText());
        }
        
        JsonNode fullItemNameNode = node.get("fullItemName");
        if (fullItemNameNode != null && fullItemNameNode.isTextual()) {
            replace.setFullItemName(fullItemNameNode.asText());
        }
        
        JsonNode columnNameNode = node.get("columnName");
        if (columnNameNode != null && columnNameNode.isTextual()) {
            replace.setColumnName(columnNameNode.asText());
        }
        
        JsonNode fullColumnNameNode = node.get("fullColumnName");
        if (fullColumnNameNode != null && fullColumnNameNode.isTextual()) {
            replace.setFullColumnName(fullColumnNameNode.asText());
        }
        
        JsonNode villageOrgNameNode = node.get("villageOrgName");
        if (villageOrgNameNode != null && villageOrgNameNode.isTextual()) {
            replace.setVillageOrgName(villageOrgNameNode.asText());
        }
        
        JsonNode townOrgIdNode = node.get("townOrgId");
        if (townOrgIdNode != null && townOrgIdNode.isTextual()) {
            replace.setTownOrgId(townOrgIdNode.asText());
        }
        
        JsonNode townOrgNameNode = node.get("townOrgName");
        if (townOrgNameNode != null && townOrgNameNode.isTextual()) {
            replace.setTownOrgName(townOrgNameNode.asText());
        }
        
        JsonNode countOrgIdNode = node.get("countOrgId");
        if (countOrgIdNode != null && countOrgIdNode.isTextual()) {
            replace.setCountOrgId(countOrgIdNode.asText());
        }
        
        JsonNode countOrgNameNode = node.get("countOrgName");
        if (countOrgNameNode != null && countOrgNameNode.isTextual()) {
            replace.setCountOrgName(countOrgNameNode.asText());
        }
        
        JsonNode cityOrgIdNode = node.get("cityOrgId");
        if (cityOrgIdNode != null && cityOrgIdNode.isTextual()) {
            replace.setCityOrgId(cityOrgIdNode.asText());
        }
        
        JsonNode cityOrgNameNode = node.get("cityOrgName");
        if (cityOrgNameNode != null && cityOrgNameNode.isTextual()) {
            replace.setCityOrgName(cityOrgNameNode.asText());
        }
        
        JsonNode provinceOrgIdNode = node.get("provinceOrgId");
        if (provinceOrgIdNode != null && provinceOrgIdNode.isTextual()) {
            replace.setProvinceOrgId(provinceOrgIdNode.asText());
        }
        
        JsonNode provinceOrgNameNode = node.get("provinceOrgName");
        if (provinceOrgNameNode != null && provinceOrgNameNode.isTextual()) {
            replace.setProvinceOrgName(provinceOrgNameNode.asText());
        }
        
        JsonNode fullOrgNameNode = node.get("fullOrgName");
        if (fullOrgNameNode != null && fullOrgNameNode.isTextual()) {
            replace.setFullOrgName(fullOrgNameNode.asText());
        }
        
        JsonNode taskNameNode = node.get("taskName");
        if (taskNameNode != null && taskNameNode.isTextual()) {
            replace.setTaskName(taskNameNode.asText());
        }
        
        JsonNode tableYearNode = node.get("tableYear");
        if (tableYearNode != null && tableYearNode.isTextual()) {
            replace.setTableYear(tableYearNode.asText());
        }
        
        JsonNode taskQuarterNode = node.get("taskQuarter");
        if (taskQuarterNode != null && taskQuarterNode.isTextual()) {
            replace.setTaskQuarter(taskQuarterNode.asText());
        }
        
        return replace;
    }
}